package com.macaline.controller.menu;

import com.macaline.bean.Result;
import com.macaline.controller.base.BaseController;
import com.macaline.entity.Permission;
import com.macaline.entity.Role;
import com.macaline.entity.RolePermission;
import com.macaline.service.perssion.PermissionService;
import com.macaline.service.perssion.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = "用户角色相关接口")
@Controller
@RequestMapping("/role")
public class RoleController {


    @Resource
    RoleService roleService;

    @Resource
    PermissionService permissionService;


    @ResponseBody
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    @ApiOperation(value = "添加角色信息", produces = "application/json")
    public Object add(Role role){
        return roleService.add(role);
    }

    @ResponseBody
    @ApiOperation(value = "为角为角色添加权限色添加权限")
    @RequestMapping(value = "addPermssionToRole",  method = RequestMethod.POST)
    public Object addPermssionToRole(RolePermission rolePermission){
        Role role = roleService.queryById(rolePermission.getRoleId());
        if(role == null){
            return Result.error("未找到角色信息");
        }
        Permission permission = permissionService.queryById(rolePermission.getPermissionId());
        if(permission == null){
            return Result.error("未找到该权限信息");
        }
        roleService.addPermissionToRole(rolePermission);
        return Result.success();
    }
}
